﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using EnvironmentManagerSystem.BLL;
using EnvironmentManagerSystem.MODE;

namespace EnvironmentManagerSystem.DepartmentManagement
{
    public partial class DepartmentManagementPermissionTo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (StfInfo.ReSession()) return;if (!IsPostBack)
            {
                BindDepartment();
                if (DDLDepartment.SelectedIndex != -1)
                {
                    BindRoleRight();
                    BindRight();
                }
            }

        }

        /// <summary>
        /// 绑定角色
        /// </summary>
        protected void BindDepartment()
        {
            DDLDepartment.DataSource = ExecutiveDepartmentService.GetExecutiveDepartmentAll();
            DDLDepartment.DataTextField = "ed_Name";
            DDLDepartment.DataValueField = "ed_Id";
            DDLDepartment.DataBind();
        }

        protected void BindRoleRight()
        {
            LBDepartment.DataSource = ExecutiveDepartmentService.GetUserList(int.Parse(DDLDepartment.SelectedValue));
            LBDepartment.DataTextField = "UserName";
            LBDepartment.DataValueField = "ID";
            LBDepartment.DataBind();
        }

        /// <summary>
        /// 绑定权限
        /// </summary>
        protected void BindRight()
        {
            LBRight.DataSource = ExecutiveDepartmentService.GetUserAll();
            LBRight.DataTextField = "UserName";
            LBRight.DataValueField = "ID";
            LBRight.DataBind();
            foreach (ListItem list in LBDepartment.Items)
            {
                LBRight.Items.Remove(LBRight.Items.FindByValue(list.Value));
            }
        }
        protected void DDLDepartment_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindRoleRight();
            BindRight();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            if (this.LBDepartment.SelectedIndex != -1)
            {
                ListItem m_Item = new ListItem(this.LBDepartment.SelectedItem.Text, this.LBDepartment.SelectedItem.Value);
                ExecutiveDepartmentService.DeleteRoleFunction(int.Parse(DDLDepartment.SelectedValue), int.Parse(LBDepartment.SelectedValue));
                if (!isTrue(LBRight, LBDepartment.SelectedItem.Value))
                {
                    this.LBRight.Items.Add(m_Item);
                }
                this.LBDepartment.Items.Remove(m_Item);
                if (LBDepartment.Items.Count > 0)
                    LBDepartment.SelectedIndex = 0;
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            if (this.LBRight.SelectedIndex != -1)
            {
                ListItem m_Item = new ListItem(this.LBRight.SelectedItem.Text, this.LBRight.SelectedItem.Value);
                if (!isTrue(LBDepartment, LBRight.SelectedItem.Value))
                {
                    ExecutiveDepartmentService.InsertRoleFunction(int.Parse(DDLDepartment.SelectedValue), int.Parse(LBRight.SelectedValue));
                    this.LBDepartment.Items.Add(m_Item);
                }
                this.LBRight.Items.Remove(m_Item);
                if (LBRight.Items.Count > 0)
                    LBRight.SelectedIndex = 0;
            }
        }

        /// <summary>
        /// 是否有重复
        /// </summary>
        /// <param name="lb">ListBoxID</param>
        /// <param name="Value">对比值</param>
        /// <returns>bool值</returns>
        private bool isTrue(ListBox lb, string Value)
        {
            bool m_temp = false;
            for (int i = 0; i < lb.Items.Count; i++)
            {
                if (lb.Items[i].Value == Value)
                {
                    return m_temp = true;
                }
            }
            return m_temp;
        }
    }
}
